package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.beans.base.image;
import com.hdax.dm.beans.item.Item;
import com.hdax.dm.beans.item.cinema;
import com.hdax.dm.dto.list.guesslikeDto;
import com.hdax.dm.feign.imagefeign;
import com.hdax.dm.mappers.list.guesslikeMapper;
import com.hdax.dm.mapstucts.list.guesslikeDtoMapper;
import com.hdax.dm.service.guesslikeService;
import com.hdax.dm.service.impl.cinemaServiceImpl;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class guesslikeServiceImpl extends ServiceImpl<guesslikeMapper, Item> implements guesslikeService {

    @Autowired
    private guesslikeDtoMapper guesslikeDtoMapper;

    @Autowired
    private imagefeign imagefeign;

    @Autowired
    private cinemaServiceImpl cinemaService;

    @Override
    public CommonResponse<List<guesslikeDto>> guesslike(Map<String,Integer> params) {
        Integer itemTypeId = params.get("itemTypeId");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("itemType1Id",itemTypeId);
        queryWrapper.last("limit 4");

        Integer cinemaId = null;

        List<Item> lists = baseMapper.selectList(queryWrapper);
        for (Item o:lists) {
            cinemaId=o.getCinemaId().intValue();
        }

        List<guesslikeDto> dtos = guesslikeDtoMapper.toTargetList(lists);
        for (guesslikeDto dto : dtos) {
            image img = imagefeign.image(dto.getId(),2L,1L);
            String imgUrl =img == null ? "":img.getImgUrl();
            dto.setImgUrl(imgUrl);

            cinema c = cinemaService.selectOne(cinemaId);

            dto.setAddress(c.getAddress());
            dto.setAreaId(c.getAreaId());
            dto.setAreaName(c.getAreaName());
        }
        return ResponseUtil.returnSuccess(dtos);
    }
}
